﻿/**
 * Copyright © 2011 looty
 * @link http://www.looty.org
 * @link http://code.google.com/p/looty/
 * @author lab9 - Bertrand Larrieu
 * @mail lab9.fr@gmail.com
 * @version 2.0
 */

package org.looty.data 
{
	import org.looty.core.disposable.*;
	
	public class MethodList implements IDisposable
	{
		
		private var _methods		:Vector.<Function>;
		private var _length			:uint;
		private var _inc			:uint;
		
		public function MethodList() 
		{
			_methods = new Vector.<Function>();
			_methods.fixed = true;
		}
		
		public function add(method:Function):void
		{
			if (method != null && _methods.indexOf(method) == -1) 
			{
				_methods.fixed = false;
				_methods[_methods.length] = method;
				_methods.fixed = true;
				_length = _methods.length;
			}
		}
		
		public function remove(method:Function):void
		{
			var i:int = _methods.indexOf(method);
			if (i != -1) 
			{
				_methods.fixed = false;
				_methods.splice(i, 1);
				_methods.fixed = true;
				_length = _methods.length;
			}
		}
		
		public function has(method:Function):Boolean
		{
			return _methods.indexOf(method) != -1;
		}
		
		public function clear():void
		{
			_methods.length = 0;
			_length = 0;
		}
		
		public function call():void
		{
			_inc = 0;			
			
			while (_inc < _length)
			{
				if (_methods[_inc] != null)
				{
					_methods[_inc]();
					++_inc;					
				}
				else
				{
					_methods.fixed = false;
					_methods.splice(_inc, 1);
					_methods.fixed = true;
					--_length;
				}
			}
		}
		
		public function dispose():void
		{
			_methods.fixed = false;
			_methods.length = 0;
			_methods = null;
			_length = 0;
		}
		
		public function get length():uint 
		{
			return _length;
		}
		
	}
	
}